package cn.iocoder.yudao.module.alipay.controller.admin.payrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.alipay.controller.admin.payrecord.vo.*;
import cn.iocoder.yudao.module.alipay.dal.dataobject.payrecord.PayRecordDO;
import cn.iocoder.yudao.module.alipay.convert.payrecord.PayRecordConvert;
import cn.iocoder.yudao.module.alipay.service.payrecord.PayRecordService;

@Tag(name = "管理后台 - 支付记录")
@RestController
@RequestMapping("/alipay/pay-record")
@Validated
public class PayRecordController {

    @Resource
    private PayRecordService payRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建支付记录")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:create')")
    public CommonResult<Long> createPayRecord(@Valid @RequestBody PayRecordCreateReqVO createReqVO) {
        return success(payRecordService.createPayRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新支付记录")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:update')")
    public CommonResult<Boolean> updatePayRecord(@Valid @RequestBody PayRecordUpdateReqVO updateReqVO) {
        payRecordService.updatePayRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除支付记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:delete')")
    public CommonResult<Boolean> deletePayRecord(@RequestParam("id") Long id) {
        payRecordService.deletePayRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得支付记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:query')")
    public CommonResult<PayRecordRespVO> getPayRecord(@RequestParam("id") Long id) {
        PayRecordDO payRecord = payRecordService.getPayRecord(id);
        return success(PayRecordConvert.INSTANCE.convert(payRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得支付记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:query')")
    public CommonResult<List<PayRecordRespVO>> getPayRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<PayRecordDO> list = payRecordService.getPayRecordList(ids);
        return success(PayRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得支付记录分页")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:query')")
    public CommonResult<PageResult<PayRecordRespVO>> getPayRecordPage(@Valid PayRecordPageReqVO pageVO) {
        PageResult<PayRecordDO> pageResult = payRecordService.getPayRecordPage(pageVO);
        return success(PayRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出支付记录 Excel")
    @PreAuthorize("@ss.hasPermission('alipay:pay-record:export')")
    @OperateLog(type = EXPORT)
    public void exportPayRecordExcel(@Valid PayRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PayRecordDO> list = payRecordService.getPayRecordList(exportReqVO);
        // 导出 Excel
        List<PayRecordExcelVO> datas = PayRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "支付记录.xls", "数据", PayRecordExcelVO.class, datas);
    }

    @GetMapping("/getHomePageCollectData")
    @Operation(summary = "查询首页汇总数据")
//    @Cacheable(value = "banner", key = "'getHomePageCollectData'")
    public CommonResult<Map<String,Object>> getHomePageCollectData(PayRecordExportReqVO pageVO) {
        return success( payRecordService.getHomePageCollectData(pageVO));
    }

}
